from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
# from django.template import loader
from django.urls import reverse
from hello.models import Question, Choice


# Create your views here.

def hello(request):
    return HttpResponse('hello')

def index(request):
    """
    投票首页处理
    :param request:
    :return:
    """
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    # template = loader.get_template('hello/index.html')
    context = {
        'latest_question_list': latest_question_list,
    }
    # return HttpResponse(template.render(context, request))
    return render(request, "hello/index.html", context)


def detail(request, question_id):
    """
    投票详细页处理方法
    :param request:
    :param question_id:
    :return:
    """
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'hello/detail.html', {'question': question})


def vote(request, question_id):
    """
    投票处理方法
    :param request:
    :param question_id:
    :return:
    """
    question = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        # Redisplay the question voting form.
        return render(request, 'hello/detail.html', {
            'question': question,
            'error_message': "You didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('hello:results', args=(question.id,)))


def results(request, question_id):
    """
    投票之后返回结果展示
    :param request:
    :param question_id:
    :return:
    """
    question = get_object_or_404(Question, pk=question_id)
    return render(request, 'hello/results.html', {'question': question})


def http_re(request):
    # print(request.method)
    # # headers = request.META
    # # print(headers)
    # # ua = request.META.get('HTTP_USER_AGENT', None)
    # # print("客户端信息:", ua)
    # # print("头信息:", request.headers)
    # # print("头信息中的浏览器信息:", request.headers['User-Agent'])
    name = request.GET.get('name', '0')
    print('获取get请求参数：', name)
    # print("请求协议信息:", request.scheme)
    # print("请求body信息:", request.body)
    # print("请求URL视图path信息:", request.path)
    # print("path_info:", request.path_info)
    # print("请求方法,大写:", request.method)
    # print("当前解码表单数据的编码字符串，或 None，表示使用 DEFAULT_CHARSET 设置）:", request.encoding)
    # print("请求的 MIME 类型的字符串:", request.content_type)
    # print("CONTENT_TYPE 头中包含的键／值参数字典:", request.content_params)
    # print("一个类似字典的对象，包含所有给定的 HTTP GET 参数:", request.GET)
    # print("一个类似字典的对象，包含所有给定的 HTTP POST 参数:", request.POST)
    # print("一个包含所有 cookies 的字典。键和值是字符串 :", request.COOKIES)
    # print("一个类似字典的对象，包含所有上传的文件 :", request.FILES)
    # # print("一个包含所有可用的 HTTP 头文件的字典 :", request.META)
    # # print("一个不区分大小写的类似字典的对象，提供对请求中所有 HTTP 前缀头的访问（加上 Content-Length 和 Content-Type）。 :", request.headers)
    # print("客户机的 IP 地址 :", request.META["REMOTE_ADDR"])
    # print("客户机的主机名 :", request.META['REMOTE_HOST'])

    return HttpResponse("响应一下")